#include <stdio.h>
#include <stdint.h>
#include <dlfcn.h>                               /* header required for dlsym() */

#include <GLES2/gl2.h>

#include "KPServer.h"

extern KPServer kpServer;

void glGenTextures(GLsizei n, GLuint* textures)
{
	static void (*my_glGenTextures)(GLsizei, GLuint*) = NULL;
	if (!my_glGenTextures)
	{
		my_glGenTextures = (void (*)(GLsizei, GLuint*)) dlsym(RTLD_NEXT, "glGenTextures");
	}
	my_glGenTextures(n, textures);

	kpServer.onGL_GenTextures(n, textures);
}

void glBindTexture(GLenum target, GLuint texture)
{
	static void (*my_glBindTexture)(GLenum, GLuint) = NULL;
	if (!my_glBindTexture)
	{
		my_glBindTexture = (void (*)(GLenum, GLuint)) dlsym(RTLD_NEXT, "glBindTexture");
	}
	my_glBindTexture(target, texture);

	kpServer.onGL_BindTexture(target, texture);
}

void glDrawArrays(GLenum mode, GLint first, GLsizei count)
{
	static void (*my_glDrawArrays)(GLenum , GLint , GLsizei ) = NULL;
	if (!my_glDrawArrays)
	{
		my_glDrawArrays = (void (*)(GLenum , GLint , GLsizei)) dlsym(RTLD_NEXT, "glDrawArrays");
	}
	my_glDrawArrays(mode, first, count);

	kpServer.onGL_DrawArrays(mode, first, count);
}